<?php

namespace App\Http\Controllers;

use App\Models\Websites;
use App\Models\WebsitesTopic;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    public $websiteId;
    public $folderStr;
    public $websiteColumnData;
    const ADMIN_PAGE_NUM = 10;
    const RESPONSE_CODE_ERROR = 1;
    const RESPONSE_CODE_SUCCESS = 0;

    public function __construct()
    {
        $this->getWebsitId();
        $this->getWebsiteColumn();
    }

    /**
     * 成功回调
     * @param string $msg 提示信息
     * @param array $data 返回数据
     * @param int $code 提示码
     * @param int $count 总数据量
     * @return mixed
     */
    protected function success(string $msg = "SUCCESS", array $data = [], int $count = 0, $code = 0)
    {
        return response()->json([
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
            'count' => $count
        ])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    /**
     * 失败回调
     * @param string $msg 提示信息
     * @param array $data 返回数据
     * @param int $code 提示码
     * @return mixed
     */
    protected function error(string $msg = 'FAIL', array $data = [], $code = 1)
    {
        return response()->json([
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    /**
     * 获取域名对应的网站id
     * @return mixed
     */
    protected  function  getWebsitId()
    {
        $websiteId = -1;
        //文件夹名称
        $folderStr = "index";
        $domainName =  Request()->getHost();
        $websiteIni['status'] = 1;
        $websiteIni['is_del'] = 1;
        $websiteIni['domains'] = $domainName;
        $websiteDatas = Websites::query()->where($websiteIni)->select("id")->first();
        if($websiteDatas){
            $websiteId = $websiteDatas->id;
            //获取模版风格id
            $websiteStyleId = DB::table('pd_websites_admin')->where('website_id', $websiteId)->value('websites_style_id');
            if($websiteStyleId >= 2){
                $folderStr = "index".$websiteStyleId;
            }
        }
        $this->websiteId = $websiteId;
        $this->folderStr = $folderStr;
    }

    /**
     * 获取网站的栏目
     * @return void
     */
    public function getWebsiteColumn()
    {
        $columnIni['website_id'] = $this->websiteId;
        $columnIni['status'] = 1;
        $columnIni['is_del'] = 1;
        $websiteColumnData = WebsitesTopic::query()->where($columnIni)->orderBy("sort")->select("id",'titles','article_count')->get()->toArray();
        if($websiteColumnData){
             $i= 2;
             foreach ($websiteColumnData as $key => $value) {
                 $websiteColumnData[$key]['indexActive'] = $i;
                 $i++;
             }
        }
        $this->websiteColumnData = $websiteColumnData;
    }


}
